package com.cold.legendary.bprocess.trigger;

import com.cold.legendary.bprocess.common.exception.BProcessException;
import com.cold.legendary.bprocess.trigger.enums.TriggerModeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cold
 * @since 2023/3/10
 */
@Slf4j
@Component
public class BProcessTriggerServiceFactory {

    private static Map<TriggerModeEnum, BProcessTriggerService> serviceMap = new ConcurrentHashMap<>();

    public void register(TriggerModeEnum triggerModeEnum, BProcessTriggerService bProcessTriggerService) {
        serviceMap.put(triggerModeEnum, bProcessTriggerService);
    }

    public BProcessTriggerService getBProcessTriggerService(TriggerModeEnum triggerModeEnum) {
        BProcessTriggerService bProcessTriggerService = serviceMap.get(triggerModeEnum);
        if (null == bProcessTriggerService) {
            log.error("找不到指定模式的服务!triggerModeEnum={}", triggerModeEnum);
            throw new BProcessException();
        }
        return bProcessTriggerService;
    }
}
